package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    //新增会员
    public void add(Member member) {
         memberDao.add(member);
    }

    /**
     * update by 黄子兴
     * @param monthRange
     * @return
     */
    @Override
    public Map<String, Object> getMemberReport(Map<String, String> monthRange) {
        List<String> months = null;
        if (monthRange == null || monthRange.isEmpty()) {
            //获取最近一年的月份
            months = DateUtils.getThisYearMonths();
        } else {
            months = DateUtils.getMonthRange(monthRange.get("start"), monthRange.get("end"));
        }
        //创建存储每月会员人数的集合
        List<Long> memberCount = new ArrayList<>();
        //获取每个月对应的会员总数
        for (String month : months) {
            month += "-01";
            String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getThisMonthLastDay(month));
            long count = memberDao.getMemberCountByMonth(thisMonthLastDay);
            memberCount.add(count);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("months" , months);
        resultMap.put("memberCount" , memberCount);
        return resultMap;
    }

    /* @create by 赵晴 * @date 2020-07-07 21:05:22 */
    //根据性别获取会员信息
    @Override
    public List<Map> getMemberReportDivBySex() {
        return memberDao.getMemberReportDivBySex();
    }
    //根据年龄段获取会员信息
    @Override
    public List<Map> getMemberReportDivByAge() {
        return memberDao.getMemberReportDivByAge();
    }
}
